 // page/realTimeData/realTimeData.js
var http = require("../../utils/http.js");
var app = getApp();
var util = require("../../utils/util.js");
var cmdUtil = require("../../utils/cmdUtil.js");
 Page({

   /**
    * 页面的初始数据
    */
   data: {
     actionSheetHidden: true,
     actionSheetItems: [
       { bindtap: 'NotWorn', txt: '未佩戴' },
       { bindtap: 'Loose', txt: '过松' },
       { bindtap: 'Comfort', txt: '舒适' },
       { bindtap: 'Tight', txt: '紧' }
     ],
     menu: '',
     //腰部压力值
     waistValue: "0",
     //背部压力值
     backValue: "0",
     receiveText: '',
     connectedDeviceId: '',
     services: {},
     characteristics: {},
     connected: true,
     background: "/image/bg.png", //背景图片
     ArrayLists: [] //数据集合
   },
   bindT:function(){
     var that = this;
     var list = that.data.array;
     list.remove(0);
     that.setData({
       array:list
     });
   },
   actionSheetTap: function () {
     this.setData({
       actionSheetHidden: !this.data.actionSheetHidden
     })
   },
   actionSheetbindchange: function () {
     this.setData({
       actionSheetHidden: !this.data.actionSheetHidden
     })
   },
   bindNotWorn: function (e) {
     this.setData({
       menu: 1,
       actionSheetHidden: !this.data.actionSheetHidden
     })
     var that=this
     let msg = cmdUtil.getCalibrationDataCmd(1, this.data.waistValue,this.data.backValue)
     console.log(msg)
     wx.writeBLECharacteristicValue({
       deviceId: that.data.connectedDeviceId,
       serviceId: that.data.services[2].uuid,
       characteristicId: that.data.characteristics[0].uuid,
       value: msg,
       success: function (ress) {
         console.log(ress);
       },
     })
   },
   bindLoose: function (e) {
     this.setData({
       menu: 2,
       actionSheetHidden: !this.data.actionSheetHidden
     })
   },
   bindComfort: function () {
     this.setData({
       menu: 3,
       actionSheetHidden: !this.data.actionSheetHidden
     })
   },
   bindTight: function () {
     this.setData({
       menu: 4,
       actionSheetHidden: !this.data.actionSheetHidden
     })
   },
   onLoad: function(options) {
     var that = this;
     let base64 = wx.getFileSystemManager().readFileSync(this.data.background, 'base64');
     that.setData({
       'background': 'data:image/jpg;base64,' + base64
     });
     this.initBlutooth()
   },
   onReady: function() {

   },

   initBlutooth:function(){
     var that = this;
     var connectedDeviceId = app.globalData.connectedDeviceId;
     if (connectedDeviceId == "" || connectedDeviceId == null) {
       wx.switchTab({
         url: '../deviceManagement/deviceManagement',
       });
     } else {
       console.log("connectedDeviceId:", connectedDeviceId);
       that.setData({
         connectedDeviceId: connectedDeviceId
       })
       wx.getBLEDeviceServices({
         deviceId: that.data.connectedDeviceId,
         success: function (res) {
           console.log(res.services)
           that.setData({
             services: res.services
           })
           wx.getBLEDeviceCharacteristics({
             deviceId: connectedDeviceId,
             serviceId: res.services[2].uuid,
             success: function (result) {
               //console.log("7",result.characteristics)
               that.setData({
                 characteristics: result.characteristics
               })
               //console.log("12",that.data.services[2].uuid);
               //console.log("13", options.connectedDeviceId);
               wx.notifyBLECharacteristicValueChange({
                 state: true,
                 deviceId: connectedDeviceId,
                 serviceId: that.data.services[2].uuid,
                 characteristicId: that.data.characteristics[0].uuid,
                 success: function (ress) {
                   console.log('启用notify成功')
                   let msg = cmdUtil.getSynchronizeDateCmd()
                   wx.writeBLECharacteristicValue({
                     deviceId: that.data.connectedDeviceId,
                     serviceId: that.data.services[2].uuid,
                     characteristicId: that.data.characteristics[0].uuid,
                     value: msg,
                     success: function (ress) {
                       console.log(ress);
                     },
                   })
                 },
                 fail: (resu) => {
                   console.log("启用notify失败");
                 }
               })
             }
           })
         }
       })
       wx.onBLEConnectionStateChange(function (res) {
         console.log(res.connected)
         if (!res.connected) {
           wx.showToast({
             title: '蓝牙设备已断开！',
           });
           wx.switchTab({
             url: '../deviceManagement/deviceManagement',
           });
         }
         that.setData({
           connected: res.connected
         })
       })
       wx.onBLECharacteristicValueChange(function (res) {
         var receiveText = app.buf2hex(res.value)
         console.log('接收到数据：' + receiveText)
         //封装数据
         var cmdType = receiveText.substr(2, 2)
         if (cmdType == '21') {
           var list = cmdUtil.dealRealTimeCommand(receiveText);
           var Jihelists = that.data.ArrayLists;
           Jihelists.push(list);
           that.setData({
             waistValue: list.lumbarPressure,
             backValue: list.backPressure,
             ArrayLists: Jihelists
           });
         }
         else if (cmdType == '23' || cmdType == '03') {
           if (cmdType == '23') {
             cmdUtil.dealHistoryData(receiveText)
           }
           else if (cmdType == '03') {
             //当前日期的发送完毕，发送下一天的
           }
         }
         else if (cmdType == '27') {
           if (receiveText.substr(4, 2) == '00') {
             wx.showToast({
               title: "设置参数成功！",
             })
           }
         }
       })
     }
   },

   onShow: function() {
     this.initBlutooth()
   },
   onHide: function() {

   }, 

   sendHistoryData: function() {
     var that = this;
     var list = that.data.ArrayLists;
     //设备ID
     var connectedDeviceId = app.globalData.connectedDeviceId;
     //header 设置
     var header = {
       token: that.data.connectedDeviceId
       //token: "ABCD0001"
     }
     //获取上传数据的上一次 数据值
     http.getReq("/spc/info/last-upload-info", "", header, (res, code) => {
       if (code == "200") {
         var lastMeasureTime
         if (res == '' || res.measureTime=='')
         {
           lastMeasureTime=util.getDateNow()
         }
         else
         {
           lastMeasureTime=res.measureTime
         }
         let msg = cmdUtil.getHistoryDataCmd(lastMeasureTime)
         wx.writeBLECharacteristicValue({
           deviceId: that.data.connectedDeviceId,
           serviceId: that.data.services[2].uuid,
           characteristicId: that.data.characteristics[0].uuid,
           value: msg,
           success: function (ress) {
             console.log(ress);
             wx.showToast({
               title: "发送成功！",
             })
           },
         })
       }
       else
       {
         wx.showToast({
           title: "网络错误！",
         })
       }
     });
   }
 })